package com.example.skillcinema.ui.screens.profile

import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageButton
import androidx.appcompat.widget.AppCompatButton
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.example.skillcinema.R
import com.example.skillcinema.adapters.CollectionRecyclerViewAdapter
import com.example.skillcinema.adapters.ProfileFilmRecyclerViewAdapter
import com.example.skillcinema.databinding.FragmentProfileBinding
import com.example.skillcinema.db.enums.FilmFetchType
import com.example.skillcinema.ui.screens.main.FilmAllFragment.Companion.FILM_FETCH_TYPE_ARG
import com.example.skillcinema.ui.screens.main.FilmDetailsFragment
import com.example.skillcinema.ui.screens.profile.viewModels.ProfileViewModel
import com.example.skillcinema.utils.sharemodels.FilmDataToShare
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ProfileFragment : Fragment() {
    private var _binding: FragmentProfileBinding? = null
    private val binding: FragmentProfileBinding
        get() = _binding!!
    private val viewModel: ProfileViewModel by viewModels<ProfileViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentProfileBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val collectionAdapter: CollectionRecyclerViewAdapter =
            CollectionRecyclerViewAdapter(
                { id, name -> showCollectionFilms(id, name) },
                { id -> removeCollection(id) })
        val watchedAdapter: ProfileFilmRecyclerViewAdapter =
            ProfileFilmRecyclerViewAdapter({ id -> showFilm(id) }, { clearWatched() })
        val historyAdapter: ProfileFilmRecyclerViewAdapter =
            ProfileFilmRecyclerViewAdapter({ id -> showFilm(id) }, { clearHistory() })
        binding.collectionRecyclerView.adapter = collectionAdapter
        binding.watchedCollectionRecyclerView.adapter = watchedAdapter
        binding.historyCollectionRecyclerView.adapter = historyAdapter
        binding.createNewCollection.setOnClickListener {
            createCollection()
        }
        binding.showAllHistoryButton.setOnClickListener {
            showCollectionFilms(-1, getString(R.string.profile_history))
        }
        binding.showAllWatchedButton.setOnClickListener {
            showCollectionFilms(3, getString(R.string.watched))
        }
        lifecycleScope.run {
            launch {
                viewModel.collectionsFlow.collect {
                    collectionAdapter.setData(it)
                }
            }
            launch {
                viewModel.watchedFlow.collect {
                    watchedAdapter.setData(it)
                }
            }
            launch {
                viewModel.historyFlow.collect {
                    historyAdapter.setData(it)
                }
            }
        }
    }

    private fun showFilm(id: Int) {
        val bundle: Bundle = Bundle().apply {
            putInt(FilmDetailsFragment.FILM_ID_ARG, id)
        }
        findNavController().navigate(R.id.action_profileFragment_to_filmDetailsFragment, bundle)
    }

    private fun clearHistory() {
        viewModel.clearHistory()
    }

    private fun clearWatched() {
        viewModel.clearWatched()
    }

    private fun removeCollection(id: Int) {
        viewModel.removeCollection(id)
    }

    private fun showCollectionFilms(id: Int, collectionName: String) {
        val bundle: Bundle = Bundle().apply {
            putParcelable(
                FILM_FETCH_TYPE_ARG,
                FilmDataToShare(
                    id,
                    FilmFetchType.PROFILE_COLLECTION,
                    null,
                    collectionName
                )
            )
        }
        findNavController().navigate(R.id.action_profileFragment_to_filmAllFragment, bundle)
    }

    private fun createCollection() {
        val dialog = Dialog(requireContext())
        dialog.setContentView(R.layout.dialog_collection_create)
        val editTextView = dialog.findViewById<EditText>(R.id.nameEditTextView)
        val createButton = dialog.findViewById<AppCompatButton>(R.id.createButton)
        val closeButton = dialog.findViewById<ImageButton>(R.id.closeButton)

        createButton.setOnClickListener {
            if (editTextView.text.isNotBlank()) {
                viewModel.createCollection(editTextView.text.toString().trim())
            }
            dialog.dismiss()
        }
        closeButton.setOnClickListener { dialog.dismiss() }
        dialog.show()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}